//
//  BezierCurveView.m
//  折线图Demo
//
//  Created by dllo on 16/8/24.
//  Copyright © 2016年 WJ. All rights reserved.
//

#import "BezierCurveView.h"
#define MARGIN    40  // 27 28 lable的宽度
#define kLeft     30  // 距离左右的距离
#define kYValue   10  // 差距的距离
#define SCREEN_W  [UIScreen mainScreen].bounds.size.width

@interface BezierCurveView ()


@property (nonatomic, assign) CGRect linerect;

@end



@implementation BezierCurveView
//- (instancetype)initWithFrame:(CGRect)frame yArray:(NSArray *)array
//{
//    self = [super initWithFrame:frame];
//    if (self) {
//        
//        self.yArray = array;
//        
//        self.linerect = frame;
//        
//        [self dravLine];
//        
//    }
//    return self;
//}
//
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
                
        self.linerect = frame;
        
        [self dravLine];
        
    }
    return self;
}

- (NSInteger)getMaxValue:(NSArray *)array
{
    NSString *str = @"0";
    for (int i = 0; i < array.count; i++) {
        
        NSString *string = array[i];
        
        if (string.integerValue > str.integerValue) {
            
            str = string;
            
        }
    }
    return [str integerValue];
}


// 根据点连线
- (void)dravLine{
    
    if (self.yArray.count !=0) {
        
        
        //2.获取目标值点坐标
        NSMutableArray *allPoints = [NSMutableArray array];
        
        for (int i = 0; i < self.yArray.count; i++) {
            
#pragma Mark- 改坐标
            // 保存坐标的x y值
            CGFloat doubleValue = [self.yArray[i] integerValue];
            
            //注意X的坐标30 是距离左边的距离
            CGFloat X = ((i * 2 + 1) / 14.0) * SCREEN_W;
                        
            //注意X的坐标230 是距离上面的距离  20 是差距的距离如 27  26 差一 * 20
            NSInteger max = [self getMaxValue:self.yArray];
            CGFloat Y = kYValue * (max - doubleValue);
            CGPoint point = CGPointMake(X,Y);
                        
            // 这洋设置有转折点
            UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(point.x -5, point.y -5, 10,10) cornerRadius:10];
            CAShapeLayer *layer = [CAShapeLayer layer];
            layer.strokeColor = [UIColor orangeColor].CGColor;
            layer.fillColor = [UIColor orangeColor].CGColor;
            layer.path = path.CGPath;
            [self.layer addSublayer:layer];
            [allPoints addObject:[NSValue valueWithCGPoint:point]];
            
        }
        
        //3.坐标连线
        UIBezierPath *path = [UIBezierPath bezierPath];
        //设置描绘的起点
        [path moveToPoint:[allPoints[0] CGPointValue]];
        
        for (int i = 1; i < allPoints.count; i++) {
            CGPoint point = [allPoints[i] CGPointValue];
            //画直线
            [path addLineToPoint:point];
        }
        
        // 线的颜色
        CAShapeLayer *shapeLayer = [CAShapeLayer layer];
        shapeLayer.path = path.CGPath;
        shapeLayer.strokeColor = [UIColor orangeColor].CGColor;
        shapeLayer.fillColor = [UIColor clearColor].CGColor;
        shapeLayer.borderWidth = 2.0;
        [self.layer addSublayer:shapeLayer];
        
        
        //4.添加目标值文字(文字也有坐标)
        CGPoint PrePonit;
        for (int i =0; i<allPoints.count; i++) {
            UILabel *label = [[UILabel alloc] init];
            label.textColor = [UIColor whiteColor];
            label.textAlignment = NSTextAlignmentCenter;
            label.font = [UIFont systemFontOfSize:20];
            [self addSubview:label];
            
            if (i==0) {
                CGPoint NowPoint = [allPoints[0] CGPointValue];
                label.text = [NSString stringWithFormat:@"%@",self.yArray[i]];
                label.frame = CGRectMake(NowPoint.x-MARGIN/2, NowPoint.y-30, MARGIN, 20);
                PrePonit = NowPoint;
            }else{
                CGPoint NowPoint = [allPoints[i] CGPointValue];
                //            if (NowPoint.y<PrePonit.y) {  //文字置于点上方
                label.frame = CGRectMake(NowPoint.x-MARGIN/2, NowPoint.y-30, MARGIN, 20);
                //            }else{ //文字置于点下方
                //                label.frame = CGRectMake(NowPoint.x-MARGIN/2, NowPoint.y, MARGIN, 20);
                //            }
                label.text = [NSString stringWithFormat:@"%@",self.yArray[i]];
                PrePonit = NowPoint;
            }
        }
        
    }
}




- (void)removeLine
{
    
}






















































































































@end
